#include "userprog/syscall.h"
#include <stdio.h>
#include <syscall-nr.h>
#include "threads/interrupt.h"
#include "threads/thread.h"

static void syscall_handler(struct intr_frame *);

void syscall_init(void) {
    intr_register_int(0x30, 3, INTR_ON, syscall_handler, "syscall");
}

void sys_exit(int status) {
    struct thread *cur = thread_current();
    cur->exit_status = status;
    thread_exit();
}

static void syscall_handler(struct intr_frame *f UNUSED) {
    int syscall_num = *(int *)f->esp;

    switch (syscall_num) {
        case SYS_EXIT: {
            int status = *((int *)f->esp + 1);
            sys_exit(status);
            break;
        }
        default:
            printf("Unknown system call!\n");
            thread_exit();
    }
}
